import axios from 'axios';
import QS from 'qs';
import store from '../store/index'
import { message } from "ant-design-vue";
import router from '@/router'

// 环境切换
axios.defaults.baseURL = 'http://47.107.231.209:2224'

// 设置请求超时
axios.defaults.timeout = 10000;

// post请求头设置
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

// 请求拦截器
axios.interceptors.request.use(
    config => {
        const token = store.state.token;
        token && (config.headers.Authorization = token);
        return config;
    },
    error => {
        return Promise.error(error);
    }
)

// 响应拦截器
axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
            if (response.data.code === 200) {
                return Promise.resolve(response);
            } else {
                switch (response.data.code) {
                    case 4004:
                        message.error("账户或密码错误");
                        break;
                    case 502:
                        message.error("没有权限");
                        router.replace({
                            path: '/login',
                        });
                        break;
                    case 500:
                        message.error("系统错误");
                        router.replace({
                            path: '/login',
                        });
                    default:
                        return Promise.resolve(response);
                }
            }
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        console.log("Request failed with status code 500");
    }
)

export function Myrequest(method, url, params) {
    if (method === "GET" || method === "get") {
        return new Promise((resolve, reject) => {
            axios.get(url, {
                params: params
            })
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    reject(err.data)
                })
        });
    } else if (method === "POST" || method === "post") {
        return new Promise((resolve, reject) => {
            axios.post(url, QS.stringify(params))
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    reject(err.data)
                })
        });
    } else if(method === "PUT" || method === "put"){
        return new Promise((resolve, reject) => {
            axios.put(url, QS.stringify(params))
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    reject(err.data)
                })
        });
    }else if(method === "DELETE" || method === "delete"){
        return new Promise((resolve, reject) => {
            axios.delete(url, {
              params: params
            })
              .then(res => {
                resolve(res.data);
              })
              .catch(err => {
                reject(err.data)
              })
          });
    }
}